package com.zxb.base.autoform.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxb.core.common.A;
import com.zxb.core.common.R;
import com.zxb.core.dao.autoform.*;
import com.zxb.base.autoform.vo.ConditionUpdateVo;
import com.zxb.core.common.Req;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author Zxb
 */
@RestController
@RequiredArgsConstructor
public class FilterController {
    private final DiyColumnDao columnDao;
    private final DiyConditionDao conditionDao;
    private final DiyFilterDao filterDao;

    @PostMapping("/filter/layout/update")
    public R updateLayout(@RequestBody ConditionUpdateVo conditionUpdateVo) {
        Long formId = conditionUpdateVo.getFormId();
        //删除原来字段布局
        conditionDao.remove(new QueryWrapper<DiyCondition>()
                .lambda()
                .eq(DiyCondition::getFormId, formId));
        List<DiyCondition> conditionList = conditionUpdateVo.getConditionList();
        conditionDao.saveBatch(conditionList);
        return R.success(conditionList.size());
    }

    @GetMapping("/filter/layout")
    public R searchLayout(Long formId) {
        List<DiyColumn> columnList = columnDao.list(new QueryWrapper<DiyColumn>()
                .lambda()
                .eq(DiyColumn::getFormId, formId));
        List<DiyCondition> conditionList = conditionDao.list(new QueryWrapper<DiyCondition>()
                .lambda()
                .eq(DiyCondition::getFormId, formId));
        return R.success(Arrays.asList(columnList, conditionList));
    }

    @GetMapping("/filter/list")
    public R list(Long formId) {
        List<DiyFilter> ownFilterList = filterDao.list(new QueryWrapper<DiyFilter>()
                .lambda()
                .eq(DiyFilter::getFormId, formId)
                .eq(DiyFilter::getUsername, Req.getCurrUser().getUsername()));
        List<DiyFilter> pubFilterList = filterDao.list(new QueryWrapper<DiyFilter>()
                .lambda()
                .eq(DiyFilter::getFormId, formId)
                .isNull(DiyFilter::getUsername));
        return R.success(Arrays.asList(ownFilterList, pubFilterList));
    }

    @PostMapping("/filter/del")
    public R del(@RequestBody List<Long> ids) {
        filterDao.removeBatchByIds(ids);
        return R.success();
    }

    @PostMapping("/filter/save")
    public R add(@RequestBody DiyFilter obj) {
        if (filterDao.exists(
                new QueryWrapper<DiyFilter>()
                        .lambda()
                        .eq(DiyFilter::getFormId, obj.getFormId())
                        .eq(DiyFilter::getName, obj.getName())
        )) {
            return R.error("过滤器名称已经存在");
        }
        obj.setUsername(Req.getCurrUser().getUsername());
        obj.setSort(0);
        filterDao.save(obj);
        return R.success();
    }

    @PostMapping("/filter/update")
    public R update(@RequestBody DiyFilter obj) {
        DiyFilter old = filterDao.getById(obj.getId());
        A.isInDatabase(old,"过滤器");
        if (filterDao.exists(
                new QueryWrapper<DiyFilter>()
                        .lambda()
                        .eq(DiyFilter::getFormId, obj.getFormId())
                        .eq(DiyFilter::getName, obj.getName())
                        .ne(DiyFilter::getId, obj.getId())
        )) {
            return R.error("过滤器名称已经存在");
        }

        old.setName(obj.getName());
        old.setCondition(obj.getCondition());
        filterDao.updateById(old);
        return R.success();
    }

    @GetMapping("/filter/count")
    public R count(Long id) {
        DiyFilter old = filterDao.getById(id);
        //根据过滤器查询数据总量
        return R.success(0);
    }
}
